
var lastinput="";

//表情框
(function(){
	
	//表情框
	/*$("#xq_con").blur(function(){
		hideAll();
		});*/
		
	}
   
)()

//有关表情的触发函数
function showToolbar(id_prefix,id){
	//alert('hello');
	//alert(id);
	
	
	$("#"+id_prefix+id+"_toolbar").show().mouseover(function(){
         $("#"+id_prefix+id+"_toolbar").unbind("blur");
    }).mouseout(function(){
       $("#"+id_prefix+id+"_toolbar").blur(function(){
           hideAll(id_prefix,id);
       });
    });
    
}

//有关表情的触发函数
/*function showToolbar(id){
	alert('in toolbar');
	$(id+"#status-toolbar").show().mouseover(function(){
         $("#xq_con").unbind("blur");
    }).mouseout(function(){
       $("#xq_con").blur(function(){
           hideAll();
       });
    });
    
}*/
function showEmotionBtns(id_prefix,id){
		//alert('in show'+id);
		$("#"+id_prefix+id+"_smilelist").show().mouseover(function(){
         $("#"+id_prefix+id).unbind("blur");
			}).mouseout(function(){
       $("#"+id_prefix+id).blur(function(){
           hideAll(id_prefix,id);
       });
    });
    $("#"+id_prefix+id).focus();
}
function hideAll(id_prefix,id){
	   $("#"+id_prefix+id+"_smilelist").hide();
	   $("#"+id_prefix+id+"_status-toolbar").hide();
}
function changeBtnImg(_this,src)
{

	$(_this).attr('src',src);
}
function insertBQ(_this,bid,id,id_prefix){
	//alert('hello');
	//alert(id);
    var emotion = $(_this).attr("emotion");
    /*var old_con = $("#"+id_prefix+id).val();
	var elem=document.getElementById(id_prefix+id);
	
	data=getCursorPosition(elem);
	
    var new_con = old_con.substring(0,data.start)//头
					+emotion+
					old_con.substring(data.end,old_con.length);
    $("#"+id_prefix+id).val(new_con);
	
	//重设光标位置
	data.start+=emotion.length;
	data.end+=emotion.length;
	data.text="";
	setCursorPosition(elem,data);
	//注意一定要继续让textarea获得焦点
	//$("#"+id_prefix+id).focus();*/
	insertStringToTextArea(emotion,document.getElementById(id_prefix+id));
	//重新计算字数
	fot(id_prefix,id);
	
	//alert(data.text+':'+data.start+':'+data.end);
}

function insertStringToTextArea(str,elem)
{
	data=getCursorPosition(elem);
	var old_con=elem.value;
    var new_con = old_con.substring(0,data.start)//头
					+str+
					old_con.substring(data.end,old_con.length);
    elem.value=new_con;
	
	//重设光标位置
	data.start+=str.length;
	data.end+=str.length;
	data.text="";
	setCursorPosition(elem,data);
}
 //字数递减和限制字数
 function fot(id_prefix,id){
		//alert("in fot"+id_prefix+id);
        //递减字数
        var d = document.getElementById(id_prefix+id+"_chars-remain");
        var c = $("#"+id_prefix+id).val().length;
		//alert(c);
        var xxx = max_chars - c;
        if(c<=max_chars){
			
            d.innerHTML =xxx;
			//alert(id_prefix+id);
			//document.getElementById(id_prefix+id).setSelectionRange(c,c);
			
        }else{
           $("#"+id_prefix+id).val( lastinput);
          $("#"+id_prefix+id).focus();
		  //document.getElementById(id_prefix+id).setSelectionRange(lastinput.length,lastinput.length);
		  //$("#"+id_prefix+id).setSelectionRange(lastinput.length,lastinput.length);
          return false;
        }
        lastinput = $("#"+id_prefix+id).val();
   }
   
 function getCursorPosition(textarea) 
 {    
	var rangeData = {text: "", start: 0, end: 0 };
	textarea.focus();
    if (textarea.setSelectionRange) 
	{ // W3C        
		rangeData.start= textarea.selectionStart;
        rangeData.end = textarea.selectionEnd;
        rangeData.text = (rangeData.start != rangeData.end) ? textarea.value.substring(rangeData.start, rangeData.end): "";
	} else if (document.selection) 
	{ // IE        
		var i,            
		oS = document.selection.createRange(),
		// Don't: oR = textarea.createTextRange()            
		oR = document.body.createTextRange();
        oR.moveToElementText(textarea);        
		rangeData.text = oS.text;        
		rangeData.bookmark = oS.getBookmark();        
		// object.moveStart(sUnit [, iCount])        
		// Return Value: Integer that returns the number of units moved.        
		for (i = 0; oR.compareEndPoints('StartToStart', oS) < 0 && oS.moveStart("character", -1) !== 0; i ++) 
		{            
			// Why? You can alert(textarea.value.length)            
			if (textarea.value.charAt(i) == '\n')
			{                
				i ++;            
			}        
		}        
		rangeData.start = i;        
		rangeData.end = rangeData.text.length + rangeData.start;    
	}    
	return rangeData;
}
function setCursorPosition(textarea, rangeData)
{    
	if(!rangeData) 
	{        
		alert("You must get cursor position first.")    
	}    
	if (textarea.setSelectionRange) 
	{ // W3C        
		textarea.focus();        
		textarea.setSelectionRange(rangeData.start, rangeData.end);    
	} 
	else if (textarea.createTextRange) 
	{ // IE        
		var oR = textarea.createTextRange();        
		// Fixbug :        
		// In IE, if cursor position at the end of textarea, the setCursorPosition function don't work        
		if(textarea.value.length === rangeData.start) 
		{            
			oR.collapse(false);
            oR.select();        
			} else {            
			oR.moveToBookmark(rangeData.bookmark);            
			oR.select();        
		}    
	}
}